package com.zb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.CardorderService;
import com.zb.Service.CarorderService;
import com.zb.Service.LeaveService;
import com.zb.Service.MoneyService;
import com.zb.Service.SealorderService;
import com.zb.Service.TaskService;
import com.zb.Service.TasktypeService;
import com.zb.Service.UserService;
import com.zb.Service.WorkService;
import com.zb.entity.Cardorder;
import com.zb.entity.Carorder;
import com.zb.entity.Leave;
import com.zb.entity.Money;
import com.zb.entity.Sealorder;
import com.zb.entity.Task;
import com.zb.entity.Tasktype;
import com.zb.entity.User;
import com.zb.entity.Work;



@Scope("prototype")
@Component
@RequestMapping("/task")
public class TaskController {
	
	@Resource
	private CardorderService cardorderService;
	
	@Resource
	private SealorderService sealorderService;
	
	@Resource
	private MoneyService moneyService;
	
	@Resource
	private LeaveService leaveService;
	
	@Resource
	private WorkService workService;
	
	@Resource
	private TaskService taskService;
	
	@Resource
	private CarorderService carorderService;
	
	@Resource
	private TasktypeService tasktypeService;
	@Resource
	private UserService us;
	
	
	
	@RequestMapping(params="op=taskid")
	public String totaskshow(int id,ModelMap modelMap){
		//任务信息
		Task task = taskService.getTaskById(id);
		//申请人信息
		User shenqing = us.getUserById(task.getUserByStartuser().getId());
		//审核人信息
		User shenhe = us.getUserById(task.getUserBySenduser().getId());
		//请假
		if(task.getTasktype().getId()==1){
			Leave leave = leaveService.getLeaveById(task.getObjectid());
			modelMap.put("leave",leave);
		}
		//出差
		if(task.getTasktype().getId()==2){
			Work work = workService.getWorkById(task.getObjectid());
			modelMap.put("work",work);
		}
		//用车
		if(task.getTasktype().getId()==3){
			Carorder carorder = carorderService.getCarorderById(task.getObjectid());
			modelMap.put("carorder",carorder);
		}
		//用章
		if(task.getTasktype().getId()==4){
			Sealorder sealorder = sealorderService.getSealorderById(task.getObjectid());
			modelMap.put("sealorder",sealorder);
		}
		//名片
		if(task.getTasktype().getId()==6){
			Cardorder cardorder = cardorderService.getCardById(task.getObjectid());
			modelMap.put("cardorder",cardorder);
		}
		//收入
		if(task.getTasktype().getId()==7){
			Money money = moneyService.getMoneyById(task.getObjectid());
			modelMap.put("money",money);
		}	
		modelMap.put("shenqing",shenqing);
		modelMap.put("shenhe",shenhe);
		modelMap.put("task",task);
		return "taskshow";
	}
	
	@RequestMapping(params="op=taskip")
	public String toUpdateTwo(Integer result,String ideal,Integer id){	
		taskService.updateTasktwo(id, ideal, result);
		return "tasktable";
	}
	
	//显示用车信息
	@RequestMapping(params = "op=taskshowshh")
	@ResponseBody
	public Map<String ,Object> showTask(Integer page, Integer rows,String min,String max,Integer tasktype,HttpSession session){
		Map<String ,Object> map=new HashMap<String, Object>();
		User user = (User)session.getAttribute("sys_user");
		if(tasktype==null){
			tasktype=0;
		}
		List<Task> data=taskService.findTask(page, rows, min, max, tasktype,user.getId());
		int count =taskService.count();
		map.put("total", count);
		map.put("rows", data);
		return map;
		
	}
	@RequestMapping(params = "op=tasktypeshow")
	@ResponseBody
	public List<Tasktype> showTasktype(){
		List<Tasktype> list=tasktypeService.findTasktype();	
		return list;
		
	}
	
	
	@RequestMapping(params = "op=TaskById")
	@ResponseBody
	public Object finddiffent(Integer taskid){
		Task task=taskService.getTaskById(taskid);
		Object object=new Object();
		if(task.getTasktype().getId()==1){
			object=leaveService.getLeaveById(task.getObjectid());
		}
		if(task.getTasktype().getId()==2){
			object=workService.getWorkById(task.getObjectid());
		}
		if(task.getTasktype().getId()==3){
			object=carorderService.getCarorderById(task.getObjectid());
		}
		if(task.getTasktype().getId()==4){
			object=sealorderService.getSealorderById(task.getObjectid());
		}
		if(task.getTasktype().getId()==6){
			object=cardorderService.getCardById(task.getObjectid());
		}
		if(task.getTasktype().getId()==7){
			object=moneyService.getMoneyById(task.getObjectid());
		}
		return object;
		
	}

	//审核信息
	@RequestMapping(params = "op=updtasks")
	@ResponseBody
	public Map<String,Object> updateCar(Integer taskids,Integer one,String ideal){
		Map<String,Object> map = new HashMap<String, Object>();
		Task task=new Task();		
		task.setState(one);
		task.setIdeal(ideal);
		task.setId(taskids);
		taskService.updateTask(task);
		map.put("success", true);
		map.put("message", "保存成功！");
		return map;	
	}
	//桌面信息展示
	@ResponseBody
	@RequestMapping(params = "op=deskshow")
	public List<Task> getDeskList(HttpSession session){
		User user = (User)session.getAttribute("sys_user");
		List<Task> tlist = taskService.getDeskList(user.getId());
		return tlist;
	}
	
	
}
